package vip.ola.pay.channel.telslowdepostwx;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import vip.ola.core.common.util.MyLog;
import vip.ola.core.common.util.PayDigestUtil;
import vip.ola.core.entity.PayOrder;
import vip.ola.core.entity.PayPassageAccount;
import vip.ola.pay.ctrl.common.BaseController;
import vip.ola.pay.mq.BaseNotify4MchPay;
import vip.ola.pay.service.RpcCommonService;

import java.math.BigDecimal;

import static vip.ola.core.common.constant.PayConstant.PAY_STATUS_COMPLETE;
import static vip.ola.core.common.constant.PayConstant.PAY_STATUS_SUCCESS;

@RequestMapping("/api/slowwx")
@Controller
public class TelwxslowPayController extends BaseController {

    private static final MyLog _log = MyLog.getLog(TelwxslowPayController.class);

    @Autowired
    private RpcCommonService rpcCommonService;
    @Autowired
    public BaseNotify4MchPay baseNotify4MchPay;

    @RequestMapping("/notify")
    @ResponseBody
    public String notify(@RequestBody JSONObject jsonParam){
        _log.info("接收TelwxwxPayController回调：" + jsonParam);
        String payOrderId = jsonParam.getString("orderNum");
        PayOrder payOrder = rpcCommonService.rpcPayOrderService.findByPayOrderId(payOrderId);
        Long orderMoney = new BigDecimal(jsonParam.getString("amount")).longValue();
        if (orderMoney.longValue() != payOrder.getAmount()){
            return "money fail";
        }
        if(payOrder == null){
            return "order not exist!";
        }
        if (payOrder.getStatus() == PAY_STATUS_SUCCESS || payOrder.getStatus() == PAY_STATUS_COMPLETE) {
            return "SUCCESS";
        }
        PayPassageAccount payPassageAccount = rpcCommonService.rpcPayPassageAccountService.findById(payOrder.getPassageAccountId());
        if(payPassageAccount == null || StringUtils.isBlank(payPassageAccount.getParam())){
            return "fail";
        }
        String retSign = jsonParam.getString("sign");
        String sign = getSign(jsonParam, payPassageAccount.getPrivateKey());
        _log.info("加签信息：map-{},sign->{}", jsonParam, sign );
        if (retSign.equals(sign)){
            int result = rpcCommonService.rpcPayOrderService.updateStatus4Success(payOrder.getPayOrderId());
            _log.info("更新订单状态为支付成功: result={}", result);
            if(result == 1){
                // 通知业务系统
                baseNotify4MchPay.doNotify(payOrder, true);
                return "SUCCESS";
            }
        }
        return "fail!";
    }

    public static String getSign(JSONObject param, String signKey){
        StringBuilder rawBuilder = new StringBuilder();
        rawBuilder.append("merchantNum").append("=").append(param.get("merchantNum")).append("&");
        rawBuilder.append("orderNum").append("=").append(param.get("orderNum")).append("&");
        rawBuilder.append("amount").append("=").append(param.get("amount")).append("&");
        rawBuilder.append("nonce_str").append("=").append(param.get("nonce_str")).append("&");
        rawBuilder.append("orderStatus").append("=").append(param.get("orderStatus")).append("&");
        rawBuilder.append("key").append("=").append(signKey);
        return PayDigestUtil.md5(rawBuilder.toString(),"UTF-8").toUpperCase();
    }
}
